Setup

Libraries

setwd(getwd())

library(MicrobeR)
## Registered S3 method overwritten by 'treeio':
##   method     from
##   root.phylo ape
## This is MicrobeR 0.3.2. See https://jbisanz.github.io/MicrobeR/ for usage information.
library(ggmap)
## Loading required package: ggplot2
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble  3.0.1     ✓ dplyr   0.8.5
## ✓ tidyr   1.0.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ✓ purrr   0.3.4
## ── Conflicts ─────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(OpenStreetMap)
## Warning: no function found corresponding to methods exports from 'raster' for:
## 'wkt'
library(Imap)
## 
## Attaching package: 'Imap'
## The following object is masked from 'package:purrr':
## 
##     imap
library(ggpubr)
## 
## Attaching package: 'ggpubr'
## The following object is masked from 'package:MicrobeR':
## 
##     mean_sd
library(vegan)
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.5-6
library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
## 
## Attaching package: 'GGally'
## The following object is masked from 'package:dplyr':
## 
##     nasa
library(RColorBrewer)
library(ggplot2)
library(readxl)

register_google(key = "AIzaSyD5BnkjpD-fHOn5IxwDz8DrAtpWjyqP7v8")

Theme

Whitecolor="#E69F00"
Chinesecolor="#0072B2"

# theme for pcoas
theme_pcoa<- function () { 
  theme_classic(base_size=10, base_family="Helvetica") +
  theme(axis.text = element_text(size=8, color = "black"), 
        axis.title = element_text(size=10, color="black"), 
        legend.text = element_text(size=8, color = "black"), 
        legend.title = element_text(size=10, color = "black"), 
        plot.title = element_text(size=10, color="black")) +
  theme(panel.border = element_rect(color="black", size=1, fill=NA))
}

World Map

manifest <- read_csv("/Volumes/turnbaughlab/qb3share/qiyanang/IDEO_manuscript_Rd2/data/IDEO_GeoCodeData.csv")
## Parsed with column specification:
## cols(
##   SampleID = col_character(),
##   SubjectID = col_character(),
##   Birthplace = col_character(),
##   Birth_Location = col_character(),
##   Age_of_migration = col_double(),
##   Duration_of_residence_SFarea = col_double()
## )
metadata<-read_excel("/Volumes/turnbaughlab/qb3share/qiyanang/IDEO_manuscript_Rd2/data/metadata_IDEO_cohort.xlsx")

check <- left_join(manifest, metadata)
## Joining, by = c("SampleID", "SubjectID", "Duration_of_residence_SFarea")
dataframe.manifest <- as.data.frame(manifest)

#The following code pulls Google maps data based on the coded birth location. Because you get charged if you do this too much, I did it and saved the data. We will skip some of the following steps 
#geocoded.data <- dataframe.manifest %>% mutate_geocode(Birth_Location)

#This creates a count for the individuals in a birth location so we can make larger or smaller shapes based on number of people, however we need to ultimately separate this out by size and shape which correspond to ethnicity and BMI
#geocoded.data$Birth_Location_Count <- as.numeric(ave(geocoded.data$Birth_Location, geocoded.data$Birth_Location, FUN=length))
#clean.geocoded.data <- as.tibble(geocoded.data)
#clean.geocoded.data$Birthplace <- NULL
#clean.geocoded.data$Duration_of_residence_SFarea <- NULL
#geo.data <- left_join(metadata, clean.geocoded.data, by = "SampleID")
#geo.data.chinese <- filter(geo.data, Ethnicity=="Chinese")
#geo.data.obese.chinese <- filter(geo.data.chinese, IDEO_BMI_Class=="Obese")
#geo.data.obese.chinese$Birth_Location_SpecificCount <- as.numeric(ave(geo.data.obese.chinese$Birth_Location, geo.data.obese.chinese$Birth_Location, FUN=length))
#geo.data.lean.chinese <- filter(geo.data.chinese, IDEO_BMI_Class=="Lean")
#geo.data.lean.chinese$Birth_Location_SpecificCount <- as.numeric(ave(geo.data.lean.chinese$Birth_Location, geo.data.lean.chinese$Birth_Location, FUN=length))
#geo.data.white <- filter(geo.data, Ethnicity=="White")
#geo.data.obese.white <- filter(geo.data.white, IDEO_BMI_Class=="Obese")
#geo.data.obese.white$Birth_Location_SpecificCount <- as.numeric(ave(geo.data.obese.white$Birth_Location, geo.data.obese.white$Birth_Location, FUN=length))
#geo.data.lean.white <- filter(geo.data.white, IDEO_BMI_Class=="Lean")
#geo.data.lean.white$Birth_Location_SpecificCount <- as.numeric(ave(geo.data.lean.white$Birth_Location, geo.data.lean.white$Birth_Location, FUN=length))
#specific.geo.data <- bind_rows(geo.data.lean.chinese, geo.data.lean.white, geo.data.obese.chinese, geo.data.obese.white)
#specific.geo.data$CountryAlpha <- 1/specific.geo.data$Birth_Location_SpecificCount

#write_csv(specific.geo.data, path = "/Users/vupadhyay/Documents/Work/UCSF/TurnbaughLab/IDEO/RMDs_and_outputs/GeoCode/Outputs/GeoCoded_BirthLocation.csv")
specific.geo.data <- read.csv("/Volumes/turnbaughlab/qb3share/qiyanang/IDEO_manuscript_Rd2/data/GeoCoded_BirthLocation.csv")

geo.data <- specific.geo.data
#world <- ne_countries(scale = "medium", returnclass = "sf")

#ggplot(data = world) +
    #geom_sf() +
  #scale_shape_manual(values = c(21)) +
  #geom_point(data = geo.data, aes(x=lon, y=lat, col = Ethnicity, fill=Ethnicity,size=Birth_Location_Count, alpha=CountryAlpha), shape=21) + scale_alpha(range = c(0.025,1))+
  #coord_sf(expand = FALSE) + scale_colour_viridis_d() + scale_fill_viridis_d() +
  #theme_bw() +
  #theme(axis.title.x = element_blank(), axis.title.y = element_blank(), legend.text = element_text(size = 14), legend.title = element_text(size = 14)) + guides(size=FALSE, alpha=FALSE)
#ggsave("Geocoded_Map_Data.png", height = 2, width = 10)

#Open maps, stamen terrain version
world_map <- openmap(c(70,-179), c(-70,179), type="nps")

#Using the autoplot feature, the world map has X and Y coordinates that convert to the lat long gride by a conversion factor of 10^7/90

#I will update a converted lat lon into the geocode matrix to adjust for this map
specific.geo.data <- specific.geo.data[order(geo.data$SampleID),]
#Getting rid of NA values for the mercator transformation
specific.geo.data <- filter(specific.geo.data, !is.na(lat))

mercator.transform.geo <- projectMercator(specific.geo.data$lat, specific.geo.data$lon)

geo.data.mercator <- cbind(specific.geo.data, mercator.transform.geo)

#For this I had a hard time figuring out the conversion
autoplot(world_map) +
  geom_point(data = geo.data.mercator, aes(x=x, y=y, shape=IDEO_BMI_Class, fill=Ethnicity,size=Birth_Location_SpecificCount, alpha=CountryAlpha)) + 
  scale_shape_manual(values = c(21, 24)) + 
  scale_size(range = c(3,6)) + 
  scale_alpha(range = c(0.1,.7)) +
  scale_fill_manual(values = c(EA=Chinesecolor, W=Whitecolor)) +
  theme_bw() +
  theme(axis.title = element_blank(), axis.text = element_blank(), legend.text = element_text(size = 10), legend.title = element_text(size = 12)) + 
  labs(size="Subjects (n)", shape="BMI") + 
  guides(alpha=FALSE,shape = guide_legend(override.aes = list(size = 5)))

#ggsave("Figures/Geocoded_World_Map.pdf", width=8, height=6, useDingbats=F)

#World Maps are just not supported by ggmap and were removed

SF Bay Area Map

#Here we did a similar thing with ZipCode data
#zipcode<-read_xlsx("/Users/vupadhyay/Documents/Work/UCSF/TurnbaughLab/IDEO/RMDs_and_outputs/Data_Inputs/Geography/IDEO microbiome zip codes.xlsx")
#Converting to datafram for the geocode mutation command
#dataframe.zipcode <- as.data.frame(zipcode)
#Converting the zipcodes to character variables as a new column
#dataframe.zipcode$zipchar <- as.character(dataframe.zipcode$zipcode)
#Getting the lat/long of the zip codes
#geocoded.zipcodes <- dataframe.zipcode %>% mutate_geocode(zipchar)
#geocoded.zipcodes$Count <- as.numeric(ave(geocoded.zipcodes$zipchar, geocoded.zipcodes$zipchar, FUN=length))
#geocoded.zipcodes$Alpha <- 1/geocoded.zipcodes$Count
#geocoded.zipcodes<-as.tibble(geocoded.zipcodes)
#geocoded.zipcodes <- rename(geocoded.zipcodes, "SampleID"="UserName")

#zipcode.data <- left_join(metadata, geocoded.zipcodes, by="SampleID")

#write_csv(zipcode.data, path = "/Users/vupadhyay/Documents/Work/UCSF/TurnbaughLab/IDEO/RMDs_and_outputs/GeoCode/Outputs/ZipcodesGeoCoded.csv")

zipcode.data <- read_csv("/Volumes/turnbaughlab/qb3share/qiyanang/IDEO_manuscript_Rd2/data/GeoCoded_Zipcodes.csv")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   SampleID = col_character(),
##   Gender = col_character(),
##   Ethnicity = col_character(),
##   IDEO_BMI_Class = col_character(),
##   MetSyn = col_character(),
##   Medication = col_character(),
##   Diabetes = col_character(),
##   Percent_BF_Class = col_character(),
##   HOMAIR_Class = col_character(),
##   Smoking = col_character(),
##   Drug_Use = col_character(),
##   USBorn = col_character(),
##   SFBorn = col_character(),
##   Education = col_character(),
##   Statin = col_character(),
##   Metformin = col_character()
## )
## See spec(...) for full column specifications.
zipcode.data <- filter(zipcode.data, !is.na(zipcode))
zipcode.data <- zipcode.data[order(zipcode.data$SampleID),]
center.sf <- c(37.7585102,-122.4539916)

#So Open street maps uses a mercator projection as opposed to lat/lon. They have a command to convert lat/lon to mercator coordinates. I order to use it, I reordered the data above to make sure order was preserved
mercator.transform <- projectMercator(zipcode.data$lat, zipcode.data$lon)
zipcode.data.mercator <- cbind(zipcode.data, mercator.transform)

#ggplot(data = world) +
    #geom_sf() +
  #geom_point(data = zipcode.data, aes(x=lon, y=lat, color=Ethnicity, shape=IDEO_BMI_Class, size=Count)) + scale_alpha(range=c(0.6,1))+ scale_size(range=c(1,2.5))+
  #coord_sf(xlim = c(-122.8,-121.8),ylim = c(37, 38.5), expand = FALSE) + scale_color_viridis_d() +
  #theme_bw() +
  #theme(axis.title.x = element_blank(), axis.title.y = element_blank(), legend.text = element_text(size = 14), legend.title = element_text(size = 14)) + guides(alpha=FALSE, size=FALSE) + labs(shape="BMI")

#ggsave("Zipcode.png")

# Manipulate these values slightly so that we get some padding on our basemap between the edge of the data and the edge of the map
# Download the basemap
sf_baymap <- get_googlemap("san francisco bay area, california", maptype = c("satellite"), zoom = 9)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=san%20francisco%20bay%20area,%20california&zoom=9&size=640x640&scale=2&maptype=satellite&key=xxx-fHOn5IxwDz8DrAtpWjyqP7v8
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=san+francisco+bay+area,+california&key=xxx-fHOn5IxwDz8DrAtpWjyqP7v8
## "san francisco bay..." not uniquely geocoded, using "san francisco bay area, ca, usa"
# Map It
ggmap(sf_baymap) + geom_point(data = zipcode.data, aes(x=lon, y=lat, fill=Ethnicity, shape=IDEO_BMI_Class), alpha=0.7, size=3) + scale_shape_manual(values = c(21, 24)) + scale_fill_manual(values = c(Chinese=Chinesecolor, White=Whitecolor)) +
  theme_bw() +
  theme(axis.text = element_blank(),axis.title.x = element_blank(), axis.title.y = element_blank(), legend.text = element_text(size = 8), legend.title = element_text(size = 10)) + guides(alpha=FALSE) + labs(shape="BMI", fill="Ethnicity")

#ggsave("Figures/Zipcode.pdf", height=2.5, width=3.5, useDingbats=F)

Geographic data boxplots

Main figure

# create an empty list
dist_list <- list()
dist_list.sf <- list()
dist_list.birth.sf <- list()
places_names <- zipcode.data$zipchar
places_df <- data.frame(names = zipcode.data$zipchar,
                        lat = zipcode.data$lat,
                        lon = zipcode.data$lon)

places.birthnames <- geo.data$Birth_Location
places.birth_df <- data.frame(names=geo.data$Birth_Location,
                              lat=geo.data$lat,
                              lon=geo.data$lon)

# iterate through data frame placing calculated distance next to place place names
for (i in 1:nrow(places_df)) {
    
    dist_list[[i]] <- gdist(lon.1 = places_df$lon[i], 
                              lat.1 = places_df$lat[i], 
                              lon.2 = places_df$lon, 
                              lat.2 = places_df$lat, 
                              units="miles")
    
}
## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used

## Warning in while (abs(lamda - lamda.old) > 1e-11) {: the condition has length >
## 1 and only the first element will be used
for (i in 1:nrow(places_df)) {
    
    dist_list.sf[[i]] <- gdist(lon.1 = places_df$lon[i], 
                              lat.1 = places_df$lat[i], 
                              lon.2 = -122.4194, 
                              lat.2 = 37.7749, 
                              units="miles")
    
}

for (i in 1:nrow(places.birth_df)) {
    
    dist_list.birth.sf[[i]] <- gdist(lon.1 = places.birth_df$lon[i], 
                              lat.1 = places.birth_df$lat[i], 
                              lon.2 = -122.4194, 
                              lat.2 = 37.7749, 
                              units="miles")
    
}

# view results as list
dist_list
## [[1]]
##  [1]  0.000000 34.960573  4.777047  2.299964 12.245733  3.986268  1.875090
##  [8]  2.299964 10.548249  3.986268  6.000936  6.000936 48.593911 34.388058
## [15] 21.939704  3.005028  2.299964  6.458114 31.687536 14.496879  9.640990
## [22]  7.358532  1.993262  5.394839 16.086817 14.425381 44.322564  5.986247
## [29]  3.005028  1.234951  4.777047  6.000936  9.640990  4.777047  3.653589
## [36]  4.799956 14.425381  6.458114  4.777047  5.731129 11.185031  3.986268
## [43] 16.445264  1.089814
## 
## [[2]]
##  [1] 35.020485  0.000000 37.136319 34.097282 38.774961 33.953100 34.799846
##  [8] 34.097282 32.412505 33.953100 34.605353 34.605353 78.967700 19.992354
## [15] 13.611810 35.807389 34.097282 37.978429  4.345995 32.105605 36.380328
## [22] 36.927202 35.926222 31.790298 37.189617 22.302535 51.470507 39.879690
## [29] 35.807389 34.069324 37.136319 34.605353 36.380328 37.136319 34.856752
## [36] 34.800779 22.302535 37.978429 37.136319 36.026844 46.012212 33.953100
## [43] 26.510291 35.212079
## 
## [[3]]
##  [1]  4.782617 37.136319  0.000000  3.674147 16.521844  3.194528  3.320689
##  [8]  3.674147  7.976907  3.194528  3.169006  3.169006 50.157743 33.850600
## [15] 24.778347  1.894089  3.674147  1.683080 34.232678 11.718761  5.534804
## [22]  2.935809  2.790762  5.358442 20.705164 18.151079 49.030703  8.618426
## [29]  1.894089  4.593616  0.000000  3.169006  5.534804  0.000000  2.338222
## [36]  2.428211 18.151079  1.683080  0.000000  1.838885 11.380148  3.194528
## [43] 15.178411  3.731216
## 
## [[4]]
##  [1]  2.2999639 34.0356893  3.6668862  0.0000000 14.4623266  1.7229049
##  [7]  0.7490831  0.0000000  8.2511419  1.7229049  3.8511839  3.8511839
## [13] 50.6785353 32.4401753 21.3665555  1.8572812  0.0000000  5.2058921
## [19] 30.9559758 12.1983232  7.6339587  5.6192591  1.8650838  3.2280302
## [25] 18.1050915 14.4765236 46.2156795  8.1161302  1.8572812  1.2998602
## [31]  3.6668862  3.8511839  7.6339587  3.6668862  1.6811223  2.7256130
## [37] 14.4765236  5.2058921  3.6668862  3.9378226 12.7349034  1.7229049
## [43] 14.2776190  1.5330978
## 
## [[5]]
##  [1] 12.247897 38.774961 16.521844 14.464161  0.000000 16.186852 14.123314
##  [8] 14.464161 22.618621 16.186852 18.249766 18.249766 40.436188 43.561012
## [15] 25.245705 15.089133 14.464161 18.093855 34.737838 26.516401 21.828178
## [22] 19.390664 13.946154 17.069122  5.106004 16.604937 32.978908  8.859470
## [29] 15.089133 13.185510 16.521844 18.249766 21.828178 16.521844 15.898617
## [36] 17.046881 16.604937 18.093855 16.521844 17.865225 13.273533 16.186852
## [43] 27.392927 13.290646
## 
## [[6]]
##  [1]  3.9863821 33.9530993  3.1945284  1.7231683 16.1868515  0.0000000
##  [7]  2.1685316  1.7231683  6.5788593  0.0000000  2.1823788  2.1823788
## [13] 52.0258899 31.3548929 21.6051436  2.1196092  1.7231683  4.3560651
## [19] 31.0383346 10.5302713  6.0188417  4.2726543  2.9112178  2.2437221
## [25] 19.8058204 15.2224664 47.8859597  9.5867627  2.1196092  3.0152046
## [31]  3.1945284  2.1823788  6.0188417  3.1945284  0.9343271  1.2329180
## [37] 15.2224664  4.3560651  3.1945284  2.6604982 13.7051823  0.0000000
## [43] 12.9413638  3.0658293
## 
## [[7]]
##  [1]  1.8754308 34.7998461  3.3206890  0.7499355 14.1233143  2.1685316
##  [7]  0.0000000  0.7499355  8.7460780  2.1685316  4.1266718  4.1266718
## [13] 50.0069111 33.1882973 22.0548578  1.4272184  0.7499355  4.9508328
## [19] 31.6910237 12.6973553  7.7956739  5.6038519  1.1320858  3.9355408
## [25] 17.9245550 15.0148261 46.1374861  7.4673654  1.4272184  1.2800945
## [31]  3.3206890  4.1266718  7.7956739  3.3206890  1.7833852  2.9269724
## [37] 15.0148261  4.9508328  3.3206890  3.9384942 11.9993735  2.1685316
## [43] 14.9572551  0.8977017
## 
## [[8]]
##  [1]  2.2999639 34.0356893  3.6668862  0.0000000 14.4623266  1.7229049
##  [7]  0.7490831  0.0000000  8.2511419  1.7229049  3.8511839  3.8511839
## [13] 50.6785353 32.4401753 21.3665555  1.8572812  0.0000000  5.2058921
## [19] 30.9559758 12.1983232  7.6339587  5.6192591  1.8650838  3.2280302
## [25] 18.1050915 14.4765236 46.2156795  8.1161302  1.8572812  1.2998602
## [31]  3.6668862  3.8511839  7.6339587  3.6668862  1.6811223  2.7256130
## [37] 14.4765236  5.2058921  3.6668862  3.9378226 12.7349034  1.7229049
## [43] 14.2776190  1.5330978
## 
## [[9]]
##  [1] 10.548292 32.412505  7.976907  8.251197 22.618621  6.578859  8.746078
##  [8]  8.251197  0.000000  6.578859  4.941892  4.941892 58.128027 26.287765
## [15] 21.818733  8.292859  8.251197  7.578576 30.208938  3.951412  4.158258
## [22]  5.824919  9.343597  5.642313 25.882787 17.782428 53.512802 16.064363
## [29]  8.292859  9.476646  7.976907  4.941892  4.158258  7.976907  7.142599
## [36]  6.091753 17.782428  7.578576  7.976907  6.141740 19.341927  6.578859
## [43]  7.565449  9.642015
## 
## [[10]]
##  [1]  3.9863821 33.9530993  3.1945284  1.7231683 16.1868515  0.0000000
##  [7]  2.1685316  1.7231683  6.5788593  0.0000000  2.1823788  2.1823788
## [13] 52.0258899 31.3548929 21.6051436  2.1196092  1.7231683  4.3560651
## [19] 31.0383346 10.5302713  6.0188417  4.2726543  2.9112178  2.2437221
## [25] 19.8058204 15.2224664 47.8859597  9.5867627  2.1196092  3.0152046
## [31]  3.1945284  2.1823788  6.0188417  3.1945284  0.9343271  1.2329180
## [37] 15.2224664  4.3560651  3.1945284  2.6604982 13.7051823  0.0000000
## [43] 12.9413638  3.0658293
## 
## [[11]]
##  [1]  6.001952 34.605353  3.169006  3.852769 18.249766  2.182379  4.126672
##  [8]  3.852769  4.941892  2.182379  0.000000  0.000000 53.217296 30.687065
## [15] 22.704579  3.394611  3.852769  3.413220 31.897093  8.822680  3.843103
## [22]  2.499554  4.499542  3.149578 21.965453 16.873099 50.067661 11.172433
## [29]  3.394611  5.152463  3.169006  0.000000  3.843103  3.169006  2.363162
## [36]  1.220611 16.873099  3.413220  3.169006  1.494943 14.522215  2.182379
## [43] 12.009967  4.977097
## 
## [[12]]
##  [1]  6.001952 34.605353  3.169006  3.852769 18.249766  2.182379  4.126672
##  [8]  3.852769  4.941892  2.182379  0.000000  0.000000 53.217296 30.687065
## [15] 22.704579  3.394611  3.852769  3.413220 31.897093  8.822680  3.843103
## [22]  2.499554  4.499542  3.149578 21.965453 16.873099 50.067661 11.172433
## [29]  3.394611  5.152463  3.169006  0.000000  3.843103  3.169006  2.363162
## [36]  1.220611 16.873099  3.413220  3.169006  1.494943 14.522215  2.182379
## [43] 12.009967  4.977097
## 
## [[13]]
##  [1] 48.61293 78.96770 50.15774 50.69679 40.43619 52.02589 50.00691 50.69679
##  [9] 58.12803 52.02589 53.21730 53.21730  0.00000 82.92616 65.58954 50.01309
## [17] 50.69679 50.81658 74.79791 61.84011 55.15344 52.65517 49.11557 53.92549
## [25] 41.85834 57.01911 47.39399 42.62448 50.01309 49.84837 50.15774 53.21730
## [33] 55.15344 50.15774 51.24308 52.14872 57.01911 50.81658 50.15774 51.99650
## [41] 38.89082 52.02589 64.95840 49.16327
## 
## [[14]]
##  [1] 34.40734 19.99235 33.85060 32.46063 43.56101 31.35489 33.18830 32.46063
##  [9] 26.28777 31.35489 30.68707 30.68707 82.92616  0.00000 23.12422 33.46523
## [17] 32.46063 33.79287 21.88324 23.60560 30.23778 32.09934 34.18976 29.26302
## [25] 44.35181 28.81706 66.04160 40.37596 33.46523 33.17791 33.85060 30.68707
## [33] 30.23778 33.85060 32.23201 31.53562 28.81706 33.79287 33.85060 32.12256
## [41] 45.05475 31.35489 18.74473 33.97459
## 
## [[15]]
##  [1] 21.981707 13.611810 24.778347 21.409722 25.245705 21.605144 22.054858
##  [8] 21.409722 21.818733 21.605144 22.704579 22.704579 65.589541 23.124220
## [15]  0.000000 23.224348 21.409722 25.885826  9.872645 22.874371 25.265034
## [22] 25.176689 23.146492 19.633640 24.074834  8.696143 42.922879 26.455751
## [29] 23.224348 21.159795 24.778347 22.704579 25.265034 24.778347 22.441206
## [36] 22.624076  8.696143 25.885826 24.778347 23.986086 32.695564 21.605144
## [43] 18.270122 22.330624
## 
## [[16]]
##  [1]  3.007438 35.807389  1.894089  1.861183 15.089133  2.119609  1.427218
##  [8]  1.861183  8.292859  2.119609  3.394611  3.394611 50.013087 33.465234
## [15] 23.224348  0.000000  1.861183  3.539367 32.786596 12.206073  6.739520
## [22]  4.365824  1.148419  4.326488 19.080767 16.364068 47.358178  7.781255
## [29]  0.000000  2.700042  1.894089  3.394611  6.739520  1.894089  1.240256
## [36]  2.202282 16.364068  3.539367  1.894089  2.777496 11.590585  2.119609
## [43] 14.994339  1.920194
## 
## [[17]]
##  [1]  2.2999639 34.0356893  3.6668862  0.0000000 14.4623266  1.7229049
##  [7]  0.7490831  0.0000000  8.2511419  1.7229049  3.8511839  3.8511839
## [13] 50.6785353 32.4401753 21.3665555  1.8572812  0.0000000  5.2058921
## [19] 30.9559758 12.1983232  7.6339587  5.6192591  1.8650838  3.2280302
## [25] 18.1050915 14.4765236 46.2156795  8.1161302  1.8572812  1.2998602
## [31]  3.6668862  3.8511839  7.6339587  3.6668862  1.6811223  2.7256130
## [37] 14.4765236  5.2058921  3.6668862  3.9378226 12.7349034  1.7229049
## [43] 14.2776190  1.5330978
## 
## [[18]]
##  [1]  6.465601 37.978429  1.683080  5.215186 18.093855  4.356065  4.950833
##  [8]  5.215186  7.578576  4.356065  3.413220  3.413220 50.816576 33.792871
## [15] 25.885826  3.539367  5.215186  0.000000 35.203940 11.051872  4.405448
## [22]  1.838896  4.473592  6.265188 22.349096 19.552927 50.685145  9.945708
## [29]  3.539367  6.230697  1.683080  3.413220  4.405448  1.683080  3.674331
## [36]  3.262415 19.552927  0.000000  1.683080  1.952168 11.928338  4.356065
## [43] 15.049890  5.410493
## 
## [[19]]
##  [1] 31.745942  4.345995 34.232678 31.015813 34.737838 31.038335 31.691024
##  [8] 31.015813 30.208938 31.038335 31.897093 31.897093 74.797914 21.883243
## [15]  9.872645 32.786597 31.015813 35.203940  0.000000 30.392071 34.013749
## [22] 34.299620 32.802687 28.949057 32.973024 18.439573 47.391211 36.327767
## [29] 32.786597 30.867905 34.232678 31.897093 34.013749 34.232678 31.914539
## [36] 31.970468 18.439573 35.203940 34.232678 33.265496 42.556016 31.038335
## [43] 25.016924 32.030059
## 
## [[20]]
##  [1] 14.496911 32.105605 11.718761 12.198361 26.516401 10.530271 12.697355
##  [8] 12.198361  3.951412 10.530271  8.822680  8.822680 61.840114 23.605598
## [15] 22.874371 12.206073 12.198361 11.051872 30.392070  0.000000  6.894022
## [22]  9.217013 13.276216  9.462699 29.635955 20.213147 56.990499 19.986778
## [29] 12.206073 13.412521 11.718761  8.822680  6.894022 11.718761 11.078973
## [36] 10.004733 20.213147 11.051872 11.718761  9.886108 22.969597 10.530271
## [43]  5.712360 13.593065
## 
## [[21]]
##  [1]  9.644803 36.380328  5.534804  7.638778 21.828178  6.018842  7.795674
##  [8]  7.638778  4.158258  6.018842  3.843103  3.843103 55.153442 30.237779
## [15] 25.265034  6.739520  7.638778  4.405448 34.013749  6.894022  0.000000
## [22]  2.630354  7.884854  6.578800 25.718607 20.211820 53.884201 14.153196
## [29]  6.739520  8.925317  5.534804  3.843103  0.000000  5.534804  6.021469
## [36]  4.912564 20.211820  4.405448  5.534804  3.967666 16.268022  6.018842
## [43] 11.621203  8.569029
## 
## [[22]]
##  [1]  7.363522 36.927202  2.935809  5.625798 19.390664  4.272654  5.603852
##  [8]  5.625798  5.824919  4.272654  2.499554  2.499554 52.655168 32.099344
## [15] 25.176689  4.365824  5.625798  1.838896 34.299620  9.217013  2.630354
## [22]  0.000000  5.476506  5.647268 23.446502 19.339427 51.716745 11.542186
## [29]  4.365824  6.835187  2.935809  2.499554  2.630354  2.935809  3.942900
## [36]  3.040059 19.339427  1.838896  2.935809  1.683491 13.767234  4.272654
## [43] 13.358222  6.272970
## 
## [[23]]
##  [1]  1.9957841 35.9262219  2.7907624  1.8677818 13.9461540  2.9112178
##  [7]  1.1320858  1.8677818  9.3435973  2.9112178  4.4995425  4.4995425
## [13] 49.1155720 34.1897614 23.1464920  1.1484191  1.8677818  4.4735919
## [19] 32.8026872 13.2762158  7.8848539  5.4765060  0.0000000  4.9311054
## [25] 17.9803891 15.9873858 46.2806104  6.7247608  1.1484191  2.0884070
## [31]  2.7907624  4.4995425  7.8848539  2.7907624  2.2018778  3.2861511
## [37] 15.9873858  4.4735919  2.7907624  3.9192413 10.9302660  2.9112178
## [43] 15.8474866  0.9972268
## 
## [[24]]
##  [1]  5.395595 31.790298  5.358442  3.229296 17.069122  2.243722  3.935541
##  [8]  3.229296  5.642313  2.243722  3.149578  3.149578 53.925488 29.263020
## [15] 19.633640  4.326488  3.229296  6.265188 28.949057  9.462699  6.578800
## [22]  5.647268  4.931105  0.000000 20.240562 13.752810 47.967505 11.331391
## [29]  4.326488  4.187396  5.358442  3.149578  6.578800  5.358442  3.178049
## [36]  3.021840 13.752810  6.265188  5.358442  4.352446 15.848614  2.243722
## [43] 11.065388  4.762420
## 
## [[25]]
##  [1] 16.095563 37.189617 20.705164 18.112870  5.106004 19.805820 17.924555
##  [8] 18.112870 25.882787 19.805820 21.965453 21.965453 41.858339 44.351809
## [15] 24.074834 19.080767 18.112870 22.349096 32.973024 29.635955 25.718607
## [22] 23.446502 17.980389 20.240562  0.000000 15.949905 28.349782 13.748370
## [29] 19.080767 16.814076 20.705164 21.965453 25.718607 20.705164 19.697482
## [36] 20.818694 15.949905 22.349096 20.705164 21.829804 18.372914 19.805820
## [43] 29.638724 17.183591
## 
## [[26]]
##  [1] 14.455594 22.302535 18.151079 14.506657 16.604937 15.222467 15.014826
##  [8] 14.506657 17.782428 15.222467 16.873099 16.873099 57.019106 28.817063
## [15]  8.696143 16.364068 14.506657 19.552927 18.439573 20.213148 20.211820
## [22] 19.339427 15.987386 13.752811 15.949905  0.000000 38.636059 18.157150
## [29] 16.364068 13.901079 18.151079 16.873099 20.211820 18.151079 15.887915
## [36] 16.408738  0.000000 19.552927 18.151079 17.853231 24.500023 15.222467
## [43] 17.478792 15.051913
## 
## [[27]]
##  [1] 44.35390 51.47051 49.03070 46.24576 32.97891 47.88596 46.13749 46.24576
##  [9] 53.51280 47.88596 50.06766 50.06766 47.39399 66.04160 42.92288 47.35818
## [17] 46.24576 50.68515 47.39121 56.99050 53.88420 51.71675 46.28061 47.96751
## [25] 28.34978 38.63606  0.00000 41.83411 47.35818 44.96420 49.03070 50.06766
## [33] 53.88420 49.03070 47.88300 48.97296 38.63606 50.68515 49.03070 50.07201
## [41] 45.25081 47.88596 55.69738 45.44433
## 
## [[28]]
##  [1]  5.988668 39.879690  8.618426  8.117918  8.859470  9.586763  7.467365
##  [8]  8.117918 16.064363  9.586763 11.172433 11.172433 42.624481 40.375957
## [15] 26.455750  7.781255  8.117918  9.945708 36.327767 19.986778 14.153196
## [22] 11.542186  6.724761 11.331391 13.748370 18.157150 41.834109  0.000000
## [29]  7.781255  7.224069  8.618426 11.172433 14.153196  8.618426  8.926290
## [36]  9.982045 18.157150  9.945708  8.618426 10.304077  6.369563  9.586763
## [43] 22.395768  6.591291
## 
## [[29]]
##  [1]  3.007438 35.807389  1.894089  1.861183 15.089133  2.119609  1.427218
##  [8]  1.861183  8.292859  2.119609  3.394611  3.394611 50.013087 33.465234
## [15] 23.224348  0.000000  1.861183  3.539367 32.786596 12.206073  6.739520
## [22]  4.365824  1.148419  4.326488 19.080767 16.364068 47.358178  7.781255
## [29]  0.000000  2.700042  1.894089  3.394611  6.739520  1.894089  1.240256
## [36]  2.202282 16.364068  3.539367  1.894089  2.777496 11.590585  2.119609
## [43] 14.994339  1.920194
## 
## [[30]]
##  [1]  1.235434 34.069323  4.593616  1.300319 13.185510  3.015205  1.280094
##  [8]  1.300319  9.476646  3.015205  5.152463  5.152463 49.848366 33.177909
## [15] 21.159795  2.700042  1.300319  6.230697 30.867905 13.412521  8.925317
## [22]  6.835187  2.088407  4.187396 16.814076 13.901079 44.964198  7.224069
## [29]  2.700042  0.000000  4.593616  5.152463  8.925317  4.593616  2.923210
## [36]  4.013235 13.901079  6.230697  4.593616  5.156620 12.319176  3.015205
## [43] 15.224351  1.173845
## 
## [[31]]
##  [1]  4.782617 37.136319  0.000000  3.674147 16.521844  3.194528  3.320689
##  [8]  3.674147  7.976907  3.194528  3.169006  3.169006 50.157743 33.850600
## [15] 24.778347  1.894089  3.674147  1.683080 34.232678 11.718761  5.534804
## [22]  2.935809  2.790762  5.358442 20.705164 18.151079 49.030703  8.618426
## [29]  1.894089  4.593616  0.000000  3.169006  5.534804  0.000000  2.338222
## [36]  2.428211 18.151079  1.683080  0.000000  1.838885 11.380148  3.194528
## [43] 15.178411  3.731216
## 
## [[32]]
##  [1]  6.001952 34.605353  3.169006  3.852769 18.249766  2.182379  4.126672
##  [8]  3.852769  4.941892  2.182379  0.000000  0.000000 53.217296 30.687065
## [15] 22.704579  3.394611  3.852769  3.413220 31.897093  8.822680  3.843103
## [22]  2.499554  4.499542  3.149578 21.965453 16.873099 50.067661 11.172433
## [29]  3.394611  5.152463  3.169006  0.000000  3.843103  3.169006  2.363162
## [36]  1.220611 16.873099  3.413220  3.169006  1.494943 14.522215  2.182379
## [43] 12.009967  4.977097
## 
## [[33]]
##  [1]  9.644803 36.380328  5.534804  7.638778 21.828178  6.018842  7.795674
##  [8]  7.638778  4.158258  6.018842  3.843103  3.843103 55.153442 30.237779
## [15] 25.265034  6.739520  7.638778  4.405448 34.013749  6.894022  0.000000
## [22]  2.630354  7.884854  6.578800 25.718607 20.211820 53.884201 14.153196
## [29]  6.739520  8.925317  5.534804  3.843103  0.000000  5.534804  6.021469
## [36]  4.912564 20.211820  4.405448  5.534804  3.967666 16.268022  6.018842
## [43] 11.621203  8.569029
## 
## [[34]]
##  [1]  4.782617 37.136319  0.000000  3.674147 16.521844  3.194528  3.320689
##  [8]  3.674147  7.976907  3.194528  3.169006  3.169006 50.157743 33.850600
## [15] 24.778347  1.894089  3.674147  1.683080 34.232678 11.718761  5.534804
## [22]  2.935809  2.790762  5.358442 20.705164 18.151079 49.030703  8.618426
## [29]  1.894089  4.593616  0.000000  3.169006  5.534804  0.000000  2.338222
## [36]  2.428211 18.151079  1.683080  0.000000  1.838885 11.380148  3.194528
## [43] 15.178411  3.731216
## 
## [[35]]
##  [1]  3.6544708 34.8567512  2.3382223  1.6830401 15.8986169  0.9343271
##  [7]  1.7833852  1.6830401  7.1425989  0.9343271  2.3631624  2.3631624
## [13] 51.2430784 32.2320050 22.4412062  1.2402556  1.6830401  3.6743310
## [19] 31.9145390 11.0789725  6.0214690  3.9429003  2.2018778  3.1780492
## [25] 19.6974818 15.8879152 47.8829951  8.9262902  1.2402556  2.9232101
## [31]  2.3382223  2.3631624  6.0214690  2.3382223  0.0000000  1.1488452
## [37] 15.8879152  3.6743310  2.3382223  2.2597519 12.8227474  0.9343271
## [43] 13.7550660  2.6149849
## 
## [[36]]
##  [1]  4.801005 34.800778  2.428211  2.727462 17.046881  1.232918  2.926972
##  [8]  2.727462  6.091753  1.232918  1.220611  1.220611 52.148718 31.535620
## [15] 22.624076  2.202282  2.727462  3.262415 31.970468 10.004733  4.912564
## [22]  3.040059  3.286151  3.021840 20.818694 16.408738 48.972960  9.982045
## [29]  2.202282  4.013235  2.428211  1.220611  4.912564  2.428211  1.148845
## [36]  0.000000 16.408738  3.262415  2.428211  1.445291 13.562121  1.232918
## [43] 12.942562  3.763368
## 
## [[37]]
##  [1] 14.455594 22.302535 18.151079 14.506657 16.604937 15.222467 15.014826
##  [8] 14.506657 17.782428 15.222467 16.873099 16.873099 57.019106 28.817063
## [15]  8.696143 16.364068 14.506657 19.552927 18.439573 20.213148 20.211820
## [22] 19.339427 15.987386 13.752811 15.949905  0.000000 38.636059 18.157150
## [29] 16.364068 13.901079 18.151079 16.873099 20.211820 18.151079 15.887915
## [36] 16.408738  0.000000 19.552927 18.151079 17.853231 24.500023 15.222467
## [43] 17.478792 15.051913
## 
## [[38]]
##  [1]  6.465601 37.978429  1.683080  5.215186 18.093855  4.356065  4.950833
##  [8]  5.215186  7.578576  4.356065  3.413220  3.413220 50.816576 33.792871
## [15] 25.885826  3.539367  5.215186  0.000000 35.203940 11.051872  4.405448
## [22]  1.838896  4.473592  6.265188 22.349096 19.552927 50.685145  9.945708
## [29]  3.539367  6.230697  1.683080  3.413220  4.405448  1.683080  3.674331
## [36]  3.262415 19.552927  0.000000  1.683080  1.952168 11.928338  4.356065
## [43] 15.049890  5.410493
## 
## [[39]]
##  [1]  4.782617 37.136319  0.000000  3.674147 16.521844  3.194528  3.320689
##  [8]  3.674147  7.976907  3.194528  3.169006  3.169006 50.157743 33.850600
## [15] 24.778347  1.894089  3.674147  1.683080 34.232678 11.718761  5.534804
## [22]  2.935809  2.790762  5.358442 20.705164 18.151079 49.030703  8.618426
## [29]  1.894089  4.593616  0.000000  3.169006  5.534804  0.000000  2.338222
## [36]  2.428211 18.151079  1.683080  0.000000  1.838885 11.380148  3.194528
## [43] 15.178411  3.731216
## 
## [[40]]
##  [1]  5.734301 36.026844  1.838885  3.942442 17.865225  2.660498  3.938494
##  [8]  3.942442  6.141740  2.660498  1.494943  1.494943 51.996502 32.122561
## [15] 23.986085  2.777496  3.942442  1.952168 33.265496  9.886108  3.967666
## [22]  1.683491  3.919241  4.352446 21.829804 17.853231 50.072010 10.304077
## [29]  2.777496  5.156620  1.838885  1.494943  3.967666  1.838885  2.259752
## [36]  1.445291 17.853231  1.952168  1.838885  0.000000 13.204380  2.660498
## [43] 13.413393  4.646592
## 
## [[41]]
##  [1] 11.199571 46.012212 11.380148 12.747687 13.273533 13.705182 11.999374
##  [8] 12.747687 19.341927 13.705182 14.522215 14.522215 38.890817 45.054752
## [15] 32.695564 11.590585 12.747687 11.928338 42.556016 22.969597 16.268022
## [22] 13.767234 10.930266 15.848614 18.372914 24.500023 45.250809  6.369563
## [29] 11.590585 12.319176 11.380148 14.522215 16.268022 11.380148 12.822747
## [36] 13.562121 24.500023 11.928338 11.380148 13.204380  0.000000 13.705182
## [43] 26.502987 11.317696
## 
## [[42]]
##  [1]  3.9863821 33.9530993  3.1945284  1.7231683 16.1868515  0.0000000
##  [7]  2.1685316  1.7231683  6.5788593  0.0000000  2.1823788  2.1823788
## [13] 52.0258899 31.3548929 21.6051436  2.1196092  1.7231683  4.3560651
## [19] 31.0383346 10.5302713  6.0188417  4.2726543  2.9112178  2.2437221
## [25] 19.8058204 15.2224664 47.8859597  9.5867627  2.1196092  3.0152046
## [31]  3.1945284  2.1823788  6.0188417  3.1945284  0.9343271  1.2329180
## [37] 15.2224664  4.3560651  3.1945284  2.6604982 13.7051823  0.0000000
## [43] 12.9413638  3.0658293
## 
## [[43]]
##  [1] 16.448618 26.510291 15.178411 14.281486 27.392927 12.941364 14.957255
##  [8] 14.281486  7.565449 12.941364 12.009967 12.009967 64.958404 18.744727
## [15] 18.270122 14.994339 14.281486 15.049890 25.016924  5.712360 11.621203
## [22] 13.358222 15.847487 11.065388 29.638724 17.478792 55.697385 22.395768
## [29] 14.994339 15.224351 15.178411 12.009967 11.621203 15.178411 13.755066
## [36] 12.942562 17.478792 15.049890 15.178411 13.413393 26.502987 12.941364
## [43]  0.000000 15.813474
## 
## [[44]]
##  [1]  1.0905521 35.2120783  3.7312159  1.5336233 13.2906462  3.0658293
##  [7]  0.8977017  1.5336233  9.6420148  3.0658293  4.9770973  4.9770973
## [13] 49.1632652 33.9745868 22.3306237  1.9201941  1.5336233  5.4104927
## [19] 32.0300583 13.5930647  8.5690286  6.2729698  0.9972268  4.7624200
## [25] 17.1835907 15.0519130 45.4443309  6.5912907  1.9201941  1.1738448
## [31]  3.7312159  4.9770973  8.5690286  3.7312159  2.6149849  3.7633682
## [37] 15.0519130  5.4104927  3.7312159  4.6465915 11.3176956  3.0658293
## [43] 15.8134744  0.0000000
dist_list.sf
## [[1]]
## [1] 2.206057
## 
## [[2]]
## [1] 34.6372
## 
## [[3]]
## [1] 3.219904
## 
## [[4]]
## [1] 0.5421093
## 
## [[5]]
## [1] 14.45091
## 
## [[6]]
## [1] 1.815859
## 
## [[7]]
## [1] 0.3532639
## 
## [[8]]
## [1] 0.5421093
## 
## [[9]]
## [1] 8.394086
## 
## [[10]]
## [1] 1.815859
## 
## [[11]]
## [1] 3.800369
## 
## [[12]]
## [1] 3.800369
## 
## [[13]]
## [1] 50.34142
## 
## [[14]]
## [1] 32.87748
## 
## [[15]]
## [1] 21.94959
## 
## [[16]]
## [1] 1.358677
## 
## [[17]]
## [1] 0.5421093
## 
## [[18]]
## [1] 4.809663
## 
## [[19]]
## [1] 31.55792
## 
## [[20]]
## [1] 12.34544
## 
## [[21]]
## [1] 7.50237
## 
## [[22]]
## [1] 5.363434
## 
## [[23]]
## [1] 1.359463
## 
## [[24]]
## [1] 3.61662
## 
## [[25]]
## [1] 18.21664
## 
## [[26]]
## [1] 15.00882
## 
## [[27]]
## [1] 46.40831
## 
## [[28]]
## [1] 7.814612
## 
## [[29]]
## [1] 1.358677
## 
## [[30]]
## [1] 1.472837
## 
## [[31]]
## [1] 3.219904
## 
## [[32]]
## [1] 3.800369
## 
## [[33]]
## [1] 7.50237
## 
## [[34]]
## [1] 3.219904
## 
## [[35]]
## [1] 1.480907
## 
## [[36]]
## [1] 2.611902
## 
## [[37]]
## [1] 15.00882
## 
## [[38]]
## [1] 4.809663
## 
## [[39]]
## [1] 3.219904
## 
## [[40]]
## [1] 3.686976
## 
## [[41]]
## [1] 12.27898
## 
## [[42]]
## [1] 1.815859
## 
## [[43]]
## [1] 14.6198
## 
## [[44]]
## [1] 1.250928
dist_list.birth.sf
## [[1]]
## [1] 6554.312
## 
## [[2]]
## [1] 6856.151
## 
## [[3]]
## [1] 653.8131
## 
## [[4]]
## [1] 0.002204385
## 
## [[5]]
## [1] 0.002204385
## 
## [[6]]
## [1] 5912.421
## 
## [[7]]
## [1] 6906.471
## 
## [[8]]
## [1] 6906.471
## 
## [[9]]
## [1] 1470.215
## 
## [[10]]
## [1] 1859.194
## 
## [[11]]
## [1] 38.61936
## 
## [[12]]
## [1] 0.002204385
## 
## [[13]]
## [1] 0.002204385
## 
## [[14]]
## [1] 2571.948
## 
## [[15]]
## [1] 6195.785
## 
## [[16]]
## [1] 0.002204385
## 
## [[17]]
## [1] 5915.934
## 
## [[18]]
## [1] 734.8628
## 
## [[19]]
## [1] 6261.761
## 
## [[20]]
## [1] 6906.471
## 
## [[21]]
## [1] 5505.781
## 
## [[22]]
## [1] 2509.159
## 
## [[23]]
## [1] 2571.948
## 
## [[24]]
## [1] 0.002204385
## 
## [[25]]
## [1] 6493.758
## 
## [[26]]
## [1] 6906.471
## 
## [[27]]
## [1] 6324.412
## 
## [[28]]
## [1] 6539.571
## 
## [[29]]
## [1] 2580.068
## 
## [[30]]
## [1] 0.002204385
## 
## [[31]]
## [1] 2113.88
## 
## [[32]]
## [1] 0.002204385
## 
## [[33]]
## [1] 0.002204385
## 
## [[34]]
## [1] 6261.761
## 
## [[35]]
## [1] 75.04434
## 
## [[36]]
## [1] 28.7549
## 
## [[37]]
## [1] 185.7608
## 
## [[38]]
## [1] 2577.493
## 
## [[39]]
## [1] NA
## 
## [[40]]
## [1] 2689.709
## 
## [[41]]
## [1] 2697.031
## 
## [[42]]
## [1] 0.002204385
## 
## [[43]]
## [1] NA
## 
## [[44]]
## [1] NA
## 
## [[45]]
## [1] 75.04434
## 
## [[46]]
## [1] 342.5506
# unlist results and convert to a "named" matrix format
dist_mat <- sapply(dist_list, unlist)
dist_mat.sf <- sapply(dist_list.sf, unlist)
dist_mat.sf <- as.matrix(dist_mat.sf) %>% cbind(as.vector(places_names)) %>% as.tibble() %>% rename("zipchar"="V2","Dist_To_SFCenter"="V1" )
## Warning: `as.tibble()` is deprecated as of tibble 2.0.0.
## Please use `as_tibble()` instead.
## The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: The `x` argument of `as_tibble.matrix()` must have column names if `.name_repair` is omitted as of tibble 2.0.0.
## Using compatibility `.name_repair`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
dist_mat.birthsf <- sapply(dist_list.birth.sf, unlist)
dist_mat.birthsf <- dist_mat.birthsf %>% as.matrix(dist_mat.birthsf) %>% cbind(as.vector(places.birthnames)) %>% as.tibble() %>% rename("Birth_Location"="V2","Dist_To_SFCenter_FromBirth"="V1" )

colnames(dist_mat) <- places_names
rownames(dist_mat) <- places_names

SFdata <- left_join(zipcode.data, dist_mat.sf) %>% distinct()
## Joining, by = "zipchar"
SFdata.merge <- select(SFdata, SampleID, Dist_To_SFCenter, Duration_of_residence_SFarea)  
  
Birthdata<- left_join(geo.data, dist_mat.birthsf) %>% distinct()
## Joining, by = "Birth_Location"
## Warning: Column `Birth_Location` joining factor and character vector, coercing
## into character vector
Birthdata.merge <- select(Birthdata, SampleID, Ethnicity, IDEO_BMI_Class, Dist_To_SFCenter_FromBirth, Age_of_migration, SFBorn, USBorn)

Geographic.Variables <- left_join(Birthdata.merge, SFdata.merge)
## Joining, by = "SampleID"
## Warning: Column `SampleID` joining factor and character vector, coercing into
## character vector
Geographic.Variables$Categories <- paste(Geographic.Variables$Ethnicity, "-", Geographic.Variables$IDEO_BMI_Class)
Geographic.Variables$Dist_To_SFCenter
##  [1]  3.8003695  3.8003695 32.8774770 21.9495934  4.8096627 31.5579186
##  [7]         NA 18.2166395  7.8146122  2.6119018  3.2199040 14.6198003
## [13]  0.3532639 12.3454419  5.3634344  1.3586772  1.4728366  3.2199040
## [19]  3.8003695  3.2199040  1.4809073 12.2789832  1.8158595  0.5421093
## [25]  0.5421093  8.3940859  1.8158595  7.5023702 15.0088201  4.8096627
## [31]  3.6869757  1.2509277         NA  2.2060566 34.6371978  3.2199040
## [37] 14.4509089  1.8158595 50.3414230  1.3586772  0.5421093  1.3594630
## [43]  3.6166198 46.4083083  7.5023702 15.0088201
Geographic.Variables$Dist_To_SFCenter_FromBirth <- as.numeric(Geographic.Variables$Dist_To_SFCenter_FromBirth)
Geographic.Variables.long <- pivot_longer(Geographic.Variables, c(Dist_To_SFCenter, Dist_To_SFCenter_FromBirth, Age_of_migration, Duration_of_residence_SFarea), names_to = "name", values_to = "values") %>% mutate(Categories=fct_relevel(Categories, "Chinese - Lean", "White - Lean", "Chinese - Obese", "White - Obese"))
## Warning: Unknown levels in `f`: Chinese - Lean, White - Lean, Chinese - Obese,
## White - Obese
Geographic.Variables.long$Rename <- factor(Geographic.Variables.long$name, levels = c("Dist_To_SFCenter", "Dist_To_SFCenter_FromBirth", "Age_of_migration", "Duration_of_residence_SFarea"), labels = c("Zipcode to SF Center", "Birth Location to SF Center", "Age at Migration (yrs)", "Duration SF Residence"), ordered = TRUE)

Geographic.Variables.long.plot <- filter(Geographic.Variables.long, name=="Dist_To_SFCenter" | name=="Duration_of_residence_SFarea")

dodge <- position_dodge(width = 1.0)
statgroups <- list(c("EA", "W"))

ggplot(data=Geographic.Variables, aes(x=IDEO_BMI_Class, y=Dist_To_SFCenter, fill=Ethnicity)) +
  geom_boxplot(outlier.size = 1) +
  scale_fill_manual(values = c(EA=Chinesecolor, W=Whitecolor))+
  stat_compare_means(method="wilcox.test", paired=FALSE)+
  theme_bw() +
  theme(axis.title= element_text(size=10), axis.title.x = element_blank(), axis.text.x = element_text(size=10), axis.text.y = element_text(size=8), legend.position = "bottom", legend.title = element_text(size=10), legend.text = element_text(size=8), panel.grid = element_blank()) +
  ylab("Distance to SF Center (miles)")
## Warning: Removed 2 rows containing non-finite values (stat_boxplot).
## Warning: Removed 2 rows containing non-finite values (stat_compare_means).

#ggsave("Figures/DistZipCode.pdf", height=3.5, width=3,useDingbats=F)

Other plots (not included in ms)

Correlations

#As you can see, the geographic data reveals that the lean white group was located closer to the "center" of SF compared to the other groups. This group also had a notably different microbiome that drove the differences between all groups

AlphaDiv <- read_tsv("/Volumes/turnbaughlab/qb3share/qiyanang/IDEO_manuscript_Rd2/data/geo_alphadiv_data.tsv") %>% select(SampleID, FaithsPD, Shannon, Richness)
## Parsed with column specification:
## cols(
##   SampleID = col_character(),
##   Ethnicity = col_character(),
##   IDEO_BMI_Class = col_character(),
##   Shannon = col_double(),
##   FaithsPD = col_double(),
##   Richness = col_double()
## )
AlphaDiv$SampleID <- sub(".V1", "", AlphaDiv$SampleID)
PhilRPC <- read_tsv("/Volumes/turnbaughlab/qb3share/qiyanang/IDEO_manuscript_Rd2/data/geo_philr_data.tsv")
## Parsed with column specification:
## cols(
##   SampleID = col_character(),
##   Axis.1 = col_double(),
##   Axis.2 = col_double(),
##   Axis.3 = col_double()
## )
PhilRPC$SampleID <- sub(".V1", "", PhilRPC$SampleID)

Microbe_With_Geography <- left_join(Geographic.Variables, AlphaDiv, by="SampleID") %>% left_join(PhilRPC, by = "SampleID")
Microbe_With_Geography.Long <- pivot_longer(Microbe_With_Geography, c(FaithsPD, Shannon, Richness, Axis.1, Axis.2, Axis.3), names_to = "Microbe_Marker", values_to = "value")

# plot distance to SF center (miles)
ggscatter(data=Microbe_With_Geography.Long, x="Dist_To_SFCenter", y="value", add.params = list(method="spearman",color="black", size = 1), cor.coef = TRUE, cor.coeff.args=list(r.digits=3, p.digits=3), facet.by = "Microbe_Marker", scales="free") +
  geom_point(aes(fill = Ethnicity), shape=21, size=2) + 
  scale_fill_manual(values = c(EA=Chinesecolor, W=Whitecolor)) + 
  xlab("Distance to SF Center (miles)") +
  theme(strip.text = element_text(size=10,color="black"), axis.text = element_text(size=8,color="black"), axis.title = element_text(size=12,color="black"), legend.position="right")
## Warning: Removed 12 rows containing non-finite values (stat_cor).
## Warning: Removed 12 rows containing missing values (geom_point).

## Warning: Removed 12 rows containing missing values (geom_point).

#ggsave("/Volumes/turnbaughlab/qb3share/qiyanang/IDEO_manuscript_Rd2/figures/geography/DistSFCenter_Stats.pdf", height=4, width=8, useDingbats=F)

# plot only correlation to Axis.1
Microbe_With_Geography.Long %>%
  filter(Microbe_Marker=="Axis.1") %>%
  ggscatter(x="Dist_To_SFCenter", y="value", add.params = list(method="spearman",color="black", size = 1), cor.coef = TRUE, cor.coeff.args=list(r.digits=3, p.digits=3)) +
  geom_point(aes(fill = Ethnicity), shape=21, size=2) + 
  scale_fill_manual(values = c(EA=Chinesecolor, W=Whitecolor)) + 
  xlab("Distance to SF Center (miles)") +
  ylab("PCoA Axis 1") +
  theme_bw() +
  theme(axis.title= element_text(size=10), axis.text.x = element_text(size=8), axis.text.y = element_text(size=8), legend.position = "top", legend.text = element_text(size=10), panel.grid = element_blank())
## Warning: Removed 2 rows containing non-finite values (stat_cor).
## Warning: Removed 2 rows containing missing values (geom_point).

## Warning: Removed 2 rows containing missing values (geom_point).

#ggsave("/Volumes/turnbaughlab/qb3share/qiyanang/IDEO_manuscript_Rd2/figures/geography/DistSFCenter_Axis1.pdf", height=3.5, width=3,useDingbats=F)

# plot duration of residence in SF (years)
ggscatter(data=Microbe_With_Geography.Long, x="Duration_of_residence_SFarea", y="value", add.params = list(method="spearman",color="black", size = 1), cor.coef = TRUE, cor.coeff.args=list(r.digits=3, p.digits=3), facet.by = "Microbe_Marker", scales="free") +
  geom_point(aes(fill = Ethnicity), shape=21, size=2) + 
  scale_fill_manual(values = c(EA=Chinesecolor, W=Whitecolor)) + 
  xlab("Distance to SF Center (miles)") +
  theme(strip.text = element_text(size=10,color="black"), axis.text = element_text(size=8,color="black"), axis.title = element_text(size=12,color="black"), legend.position="right")
## Warning: Removed 24 rows containing non-finite values (stat_cor).
## Warning: Removed 24 rows containing missing values (geom_point).

## Warning: Removed 24 rows containing missing values (geom_point).

#ggsave("/Volumes/turnbaughlab/qb3share/qiyanang/IDEO_manuscript_Rd2/figures/geography/DurationInSF_Stats.pdf", height=4, width=8, useDingbats=F)

# plot only correlation to Axis.1
Microbe_With_Geography.Long %>%
  filter(Microbe_Marker=="Axis.1") %>%
  ggscatter(x="Duration_of_residence_SFarea", y="value", add.params = list(method="spearman",color="black", size = 1), cor.coef = TRUE, cor.coeff.args=list(r.digits=3, p.digits=3)) +
  geom_point(aes(fill = Ethnicity), shape=21, size=2) + 
  scale_fill_manual(values = c(EA=Chinesecolor, W=Whitecolor)) + 
  xlab("Duration of residence (years)") +
  ylab("PCoA Axis 1") +
  theme_bw() +
  theme(axis.title= element_text(size=10), axis.text.x = element_text(size=8), axis.text.y = element_text(size=8), legend.position = "top", legend.text = element_text(size=10), panel.grid = element_blank())
## Warning: Removed 4 rows containing non-finite values (stat_cor).
## Warning: Removed 4 rows containing missing values (geom_point).

## Warning: Removed 4 rows containing missing values (geom_point).

#ggsave("/Volumes/turnbaughlab/qb3share/qiyanang/IDEO_manuscript_Rd2/figures/geography/DurationSFArea_Axis1.pdf", height=3.5, width=3,useDingbats=F)